import router from '@/router/index.js'
import store from '@/store/index.js'
import nprogress from 'nprogress/nprogress.js'
import 'nprogress/nprogress.css'

router.beforeEach(async(to, from, next) => {
  nprogress.start()
  // 判断token有没有
  if (store.getters.token) {
    // 如果是登录页面跳到首页
    // 如果用户资料没有,获取用户资料
    if (!store.getters.userId) {
      await store.dispatch('user/getUserInfo')
    }
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 在白名单里面跳过去
    if (['/login', '/404'].includes(to.path)) {
      next()
    } else {
      // 跳转到登录页面
      next('/login')
    }
  }
  nprogress.done()
})
router.afterEach(() => {
  nprogress.done()
})
